package superAdmin;

import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.MethodOrderer;
import org.junit.jupiter.api.Order;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestMethodOrder;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.CsvFileSource;
import org.junit.jupiter.params.provider.CsvSource;
import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;

import java.lang.management.MemoryType;
import java.util.List;
import java.util.concurrent.TimeUnit;

import static java.lang.Thread.sleep;

/*
    班级的增删改查
 */
@TestMethodOrder(MethodOrderer.OrderAnnotation.class)
public class addClass extends InitAdmin{
    @ParameterizedTest
    @CsvFileSource(resources = "../superadminLogin.csv")
    @Order(1)
    void login(String name,String password,String loginURL,String toURL) throws InterruptedException {
        //获取登录页
        webDriver.get(loginURL);
        webDriver.manage().window().maximize();
        webDriver.manage().timeouts().implicitlyWait(3, TimeUnit.DAYS);
        System.out.println("username:"+name);
        System.out.println("password:"+password);
        //输入账号密码
        webDriver.findElement(By.cssSelector("#login > div.logIn-inner > div.right-con.fr > div > div.login-form > form > div:nth-child(1) > div > div > input")).sendKeys(name);
        webDriver.findElement(By.cssSelector("#login > div.logIn-inner > div.right-con.fr > div > div.login-form > form > div:nth-child(2) > div > div > input")).sendKeys(password);
        //点击登录
        webDriver.findElement(By.cssSelector("#login > div.logIn-inner > div.right-con.fr > div > div.login-form > form > div:nth-child(3) > div > button")).click();
        webDriver.manage().window().maximize();
        sleep(3000);

        String curUrl = webDriver.getCurrentUrl();
        Assertions.assertEquals(toURL,curUrl);
        sleep(3000);

    }
    @ParameterizedTest
    @CsvSource("计科,1001")
    @Order(2)
    void addCollege(String collegeName,String  collegeId){
        //点击机构管理
        webDriver.findElement(By.cssSelector("#admin > div.main > div > div.left-menu > ul > li:nth-child(2) > ul > li.on")).click();
        //选择 添加学院
        webDriver.findElement(By.cssSelector("#schoolBox > div.tit > div:nth-child(2) > button:nth-child(2) > span")).click();
        //输入学院和编号
        webDriver.findElement(By.cssSelector("#admin > div.main > div > div.el-dialog__wrapper.editAcademy > div > div.el-dialog__body > div > form > div:nth-child(1) > div > div > input"))
                .sendKeys(collegeName);
        webDriver.findElement(By.cssSelector("#admin > div.main > div > div.el-dialog__wrapper.editAcademy > div > div.el-dialog__body > div > form > div:nth-child(2) > div > div > input"))
                .sendKeys(collegeId);
        //点击确定
        webDriver.findElement(By.cssSelector("#admin > div.main > div > div.el-dialog__wrapper.editAcademy > div > div.el-dialog__footer > div > button.el-button.btn.confirm-btn.el-button--primary.el-button--small"))
                .click();
    }


    @ParameterizedTest
    @CsvSource("计科,1班")
    @Order(3)
    void addClass(String collegeName,String className) throws InterruptedException {
        sleep(3000);
        //获取 页面所有的机构名元素 验证是否删除成功
        List<WebElement> colleges = webDriver.findElements(By.cssSelector("h1 > font"));
        //#el-collapse-head-3562 > h1 > font
        System.out.println(colleges.size());
        for (int i = 0; i < colleges.size(); i++) {
            if (colleges.get(i).getText().equals(collegeName)){
                colleges.get(i).click();
            }
        }
        //选择 添加班级
        webDriver.findElement(By.cssSelector("#schoolBox > div.tit > div:nth-child(3) > button:nth-child(2) > span")).click();
        //输入班级和id
        String classId = String.valueOf((int) ((Math.random()*100)+1));
        webDriver.findElement(By.cssSelector("#admin > div.main > div > div.el-dialog__wrapper.editClass > div > div.el-dialog__body > div > form > div:nth-child(1) > div > div > input"))
                        .sendKeys(className);
        webDriver.findElement(By.cssSelector("#admin > div.main > div > div.el-dialog__wrapper.editClass > div > div.el-dialog__body > div > form > div:nth-child(2) > div > div > input"))
                .sendKeys(classId);
        //点击确定
        webDriver.findElement(By.cssSelector("#admin > div.main > div > div.el-dialog__wrapper.editClass > div > div.el-dialog__footer > div > button.el-button.btn.confirm-btn.el-button--primary.el-button--small > span"))
                .click();
    }
    @Test
    @Order(4)
    void search(){
        List<WebElement> classes =  webDriver.findElements(By.cssSelector(".class-list>ul em"));
        System.out.println("班级总数:"+classes.size());
        for (int i = 0; i <classes.size(); i++) {
            System.out.println("当前班级是:"+classes.get(i).getText());
        }
    }
    @Test
    @Order(5)
    void deleteClass() throws InterruptedException {
        //选择刚才新添加的班级
        sleep(3000);
        webDriver.findElement(By.cssSelector("#schoolBox > div.class-list > ul > li")).click();
        //点击删除
        webDriver.findElement(By.cssSelector("#schoolBox > div.classInfo > form > div.btnList > button.el-button.btn.deleteBtn.el-button--default.el-button--small > span"))
                .click();
        //点击确定
        webDriver.findElement(By.cssSelector("body > div.el-message-box__wrapper > div > div.el-message-box__btns > button.el-button.el-button--default.el-button--small.el-button--primary.confirm-btn > span"))
                .click();
        sleep(3000);
    }
    @ParameterizedTest
    @CsvSource("计科")
    @Order(6)
    void deleteCollege(String collegeName) throws InterruptedException {
        sleep(3000);
        //获取 页面所有的机构名元素 验证是否删除成功
        List<WebElement> colleges = webDriver.findElements(By.cssSelector("h1 > font"));
        //#el-collapse-head-3562 > h1 > font
        System.out.println(colleges.size());
        for (int i = 0; i < colleges.size(); i++) {
            if (colleges.get(i).getText().equals(collegeName)){
                colleges.get(i).click();
            }
        }
        webDriver.findElement(By.cssSelector("#schoolBox > div.tit > div:nth-child(3) > button:nth-child(1)")).click();
        //点击删除
        webDriver.findElement(By.cssSelector("#admin > div.main > div > div.el-dialog__wrapper.editAcademy > div > div.el-dialog__footer > div > button.el-button.btn.delete-btn.el-button--primary.el-button--small"))
                .click();
        //确定 删除
        webDriver.findElement(By.cssSelector("body > div.el-message-box__wrapper > div > div.el-message-box__btns > button.el-button.el-button--default.el-button--small.el-button--primary.confirm-btn"))
                .click();
        //获取 页面所有的机构名元素 验证是否删除成功
        sleep(3000);
        colleges = webDriver.findElements(By.cssSelector("h1 > font"));
        //#el-collapse-head-3562 > h1 > font
        System.out.println(colleges.size());
        for (int i = 0; i < colleges.size(); i++) {
            String name = colleges.get(i).getText();
            System.out.println("当前学院机构："+name);
            if (name.equals(collegeName)) {
                System.out.println("测试不通过!删除失败，依旧存在:"+colleges.get(i));
            }
        }
    }
}
